package com.example.programme;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.programme.database.UserDBHelper;
import com.example.programme.database.UserManager;
import com.example.programme.home.HomeActivity;
import com.example.programme.Items.UserItem;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "提示";
    //设置页面控件
    Button button1;//注册
    Button button2;//登录
    private EditText et_name,et_password;
    UserDBHelper userDBHelper;
    UserManager userManager;
    String name,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //绑定控件
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        et_name = findViewById(R.id.et_name);
        et_password=findViewById(R.id.et_password);
        //
        userDBHelper = new UserDBHelper(LoginActivity.this);
       // SQLiteDatabase db=userDBHelper.getWritableDatabase();

        SharedPreferences sharedPreferences = getSharedPreferences("User_Account",
                Activity.MODE_PRIVATE);
        name=sharedPreferences.getString("name",null);
        password=sharedPreferences.getString("password",null);
        et_name.setText(name);
        et_password.setText(password);

    }
    /*点击注册
    *
     */
    public void button1(View btn){
        Log.i(TAG,"open :打开注册界面");
        Intent intent = new Intent(this,RegisterActivity.class);
        startActivityForResult(intent,1);//requestcode ,resultCode确定返回的是什么数据项
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode==1&&resultCode==2) {
            //汇率修改页面返回的数据，data中包含bundle对象
            Bundle bdl = data.getExtras();
            //拆分放入的数据
            String name=bdl.getString("name_key",null);
            String password=bdl.getString("password_key",null);

            //保存新的账号数据到sharep..User_Account是数据保存的文件夹
            SharedPreferences sp = getSharedPreferences("User_Account", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("name", name);
            editor.putString("password", password);
            editor.apply();
            //显示
            et_name.setText(sp.getString("name",null));
            et_password.setText(sp.getString("password",null));


        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    /*点击登录
     *
     */
    public void button2(View btn){

        String name = et_name.getText().toString().trim();
        String password = et_password.getText().toString().trim();

        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {

            UserManager userManager=new UserManager(LoginActivity.this);//易忘记
            //ArrayList<UserItem> data =  userManager.listAll();//有问题
            boolean userdata = false;
            for (UserItem user:userManager.listAll()) {
                //UserItem user= data.get(i);   //可存储账号数量
                if (name.equals(user.getName()) && password.equals(user.getPassword())) {
                    userdata = true;
                    break;
                } else {
                    userdata = false;
                }
            }
            if (userdata){
                Toast.makeText(LoginActivity.this,"登陆成功!",Toast.LENGTH_LONG).show();
                Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                startActivity(intent);//开始下一个页面
                finish();
            }
            else{
                Toast.makeText(LoginActivity.this,"用户名或密码错误!",Toast.LENGTH_LONG).show();
            }
        }else {
            Toast.makeText(LoginActivity.this,"用户名或密码不能为空!",Toast.LENGTH_LONG).show();
        }
    }
};
